﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VueEntities;
using VueIRepository;
using VueModel;

namespace VueRepository
{
    public class UserRepository : IUserRepository
    {
        private VueDbContext _db;
        public UserRepository(VueDbContext db)
        {
            _db = db;
        }
        //Password字段不应该在User Entities中，应该有一个专门的登录 Entities。

        //获取用户信息列表
        public List<UserModel> GetUserList()
        {
            var user1List = _db.User.Select(s => new UserModel
            {
                UserId = s.UserId,
                Name = s.Name,
                Address = s.Address,
                Birthday = s.Birthday,
                Age = s.Age,
                Email = s.Email,
                TelPhone = s.TelPhone,
                Sex = s.Sex,
                Role = s.UserAndRole.Select(ur => ur.Role.RoleName).ToList()
            }).ToList();

            return user1List;
        }
        //添加用户信息
        public bool AddUser(UserModel model)
        {
            var tran = _db.Database.BeginTransaction();
            try
            {
                //先在User表中添加相关行
                var user = _db.User.FirstOrDefault(s => s.Email == model.Email);
                if(user == null)
                {
                    var addUser = new User
                    {
                        Name = model.Name,
                        Address = model.Address,
                        Birthday = model.Birthday,
                        Age = model.Age,
                        Email = model.Email,
                        TelPhone = model.TelPhone,
                        Password = model.Password,
                        Sex = model.Sex
                    };
                    _db.User.Add(addUser);
                    _db.SaveChanges();               
                    //再在关联表中添加相关行
                    var list = new List<UserAndRole>();
                    foreach(var i in model.RoleId)
                    {
                        var userAndRole = new UserAndRole { UserId = addUser.UserId,RoleId = i };
                        list.Add(userAndRole);
                    }
                    _db.UserAndRole.AddRange(list);
                    _db.SaveChanges();                   
                }
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }
        //删除用户
        public bool DelUser(int userId)
        {
            var tran = _db.Database.BeginTransaction();
            try
            {
                //先删除关联表中的相关行
                var userAndRole = _db.UserAndRole.Where(s => s.UserId == userId).ToList();
                _db.UserAndRole.RemoveRange(userAndRole);
                _db.SaveChanges();
                //再删除User表中的相关行
                var user = _db.User.FirstOrDefault(s => s.UserId == userId);
                _db.User.Remove(user);
                _db.SaveChanges();
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }
        //修改用户
        public bool ModefyUser(UserModel model)
        {
            var tran = _db.Database.BeginTransaction();
            try
            {
                //删除关联表中的相关内容
                var userAndRole = _db.UserAndRole.Where(s => s.UserId == model.UserId).ToList();
                _db.UserAndRole.RemoveRange(userAndRole);
                _db.SaveChanges();
                //在User表中修改
                var user = _db.User.FirstOrDefault(s => s.UserId == model.UserId);
                user.Name = model.Name;
                user.Address = model.Address;
                user.Birthday = model.Birthday;
                user.Age = model.Age;
                user.Email = model.Email;
                user.TelPhone = model.TelPhone;
                user.Sex = model.Sex;
                _db.SaveChanges();
                //在关联表中添加行
                var list = new List<UserAndRole>();
                foreach (var i in model.RoleId)
                {
                    var userandRole = new UserAndRole { UserId = user.UserId, RoleId = i };
                    list.Add(userandRole);
                }
                _db.UserAndRole.AddRange(list);
                _db.SaveChanges();
                tran.Commit();
                return true;
            }
            catch
            {
                tran.Rollback();
                return false;
            }
        }
    }
}
